<?php
	/**
	 * URI Information.
	 * 
	 * Implements an object for getting information about the URI provided to an
	 * HTTP server.
	 *	 
	 * @author     Brandon R. Stoner <brandon.stoner@gmail.com>
	 * @copyright  Copyright 2008, Monokromatic Inc.
	 * @package    Components
	 * @subpackage Routing
	 **/

	/**
	 * Basic URI information
	 * 
	 * Stores information related to the URI provided to the server, and allows
	 * access to different values related to it.
	 * 	 	  
	 * @author     Brandon R. Stoner <brandon.stoner@gmail.com>
	 * @copyright  Copyright 2008, Monokromatic Inc.
	 * @package    Components
	 * @subpackage URI
	 **/
	class CKromeURIInfoComponent extends IKromeComponent
	{
		/**
		 * Array of segments passed to the server, without variable segments
		 * @var array		 
		 **/		 		
		private $segments     = Array();

		/**
		 * Array of variable data passed through the URI
		 * @var array		 
		 **/		 		
		private $variables    = Array();

		/**
		 * Initializes variables in the object
		 * @see IKromeComponent::__construct()
		 **/		 		
		public function __construct( $componentManager = null )
		{
				// Call our parent constructor
			parent::__construct( $componentManager );

				// Store the original URI provided
			$this->originalURI = $_SERVER['REQUEST_URI'];

				// Temporary variable storing the complete segment list
			$segmentList       = explode( '/', $_SERVER['REQUEST_URI'] );

			while ( ($segment = array_shift( $segmentList )) !== NULL )
			{
					// This was the root segment
				if ( $segment === '' )
				{
					continue;
				}

					// If this is formatted like a variable...
				$matches = Array();
				if ( preg_match('/(.+)\=(.+)/', $segment, $matches) )
				{
						// ...convert strings into things they should be...
					switch ( $matches[2] )
					{
						case 'false':
							$matches[2] = false;
							break;

						case 'true':
							$matches[2] = true;
							break;
					}

						// ...and recognize this as a variable.
					$this->variables[ $matches[1] ] = $matches[ 2 ];
				}
				else
				{
						// Otherwise, this is a normal segment.
					array_push( $this->segments, $segment );
				}
			}
		}

		/**
		 * Return a segment based on it's order in the URI as a number.
		 * @param integer $pSegment Number representing this segment in order from 1		 
		 * @return string The value of the given segment, or false if doesn't exist		 		 
		 **/		 		
		public function Segment( $pSegment = null )
		{
			$retVal = false;

			if ( $pSegment === null )
			{
				$retVal = $this->segments;
			}
			else
			{
				if ( is_integer($pSegment) && isset($this->segments[$pSegment-1]) )
				{
					$retVal = $this->segments[$pSegment-1];
				}
			}

			return $retVal;
		}

		/**
		 * Return a variable that was passed through the URI
		 * @param string $pVariable Name of the variable being requested		 
		 * @return mixed The value of the requested variable, or NULL if the
		 *               variable doesn't exist.
		 **/		 		
		public function Variable( $pVariable = null )
		{
			$retVal = null;

			if ( isset($this->variables[$pVariable]) ) {
   			$retVal = $this->variables[$pVariable];
   		}

			return $retVal;
		}

		/**
		 * @see IKromeComponent::Register()		
		 **/		
		public function Register()
		{
			if ( !KROME_CLI )
			{
				$this->RegisterAbility( 'routing' );
			}
		}

		/**
		 * @see IKromeComponent::Cleanup()
		 **/		 		
		public function Cleanup()
		{
		}
	}
?>
